package br.com.canvasForAll.ws.resource;

import java.util.Collection;
import java.util.Date;

import javax.ejb.EJB;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;

import br.com.canvasForAll.model.entity.Canvas;
import br.com.canvasForAll.model.entity.Perfil;
import br.com.canvasForAll.model.entity.Postit;
import br.com.canvasForAll.model.services.interfaces.CanvasService;
import br.com.canvasForAll.model.services.interfaces.PerfilService;
import br.com.canvasForAll.model.services.interfaces.PostitService;
import br.com.canvasForAll.model.utils.json.JSONObject;

@Path("postit")
public class PostitWS {
	
	@EJB private PostitService postitService;
	@EJB private CanvasService canvasService;
	@EJB private PerfilService perfilService;
	
	/**
	 * @param String id_canvas_ics
	 * @return Collection<Postit>
	 * */
	@GET
	@Path("/postitCanvas/{idCanvas}")
	@Produces(MediaType.APPLICATION_JSON)
	public Collection<Postit> getPostitCanvas(@PathParam("idCanvas") String idCanvas) throws Exception {
		try{
			Long id = new Long(idCanvas);
			Canvas canvas = canvasService.obterPorId(id);
			Collection<Postit> postits =  postitService.obterPostitCanvas(canvas);
			return postits;
		
		} catch (WebApplicationException e) {
			e.printStackTrace();
			throw new WebApplicationException(Status.NOT_FOUND);

		} catch (NumberFormatException e){
			e.printStackTrace();
			throw new WebApplicationException(Status.BAD_REQUEST);

		}
	}
	
	/**
	 * @param [JSON] {"titulo":"valor", "descricao":"valor", "dataCriacao":"valor", "dataUltimaAtualizacao":"valor"}
	 * @param String id_perfil_ics
	 * @param String id_canvas_ics
	 * @param String id_quadrante
	 * @param String id_cor
	 * */	
	@POST
	@Path("/addPostit/{id_perfil_ics}/{id_canvas_ics}/{id_quadrante}/{id_cor}")
	@Consumes()
	public Response addPostit(
			@PathParam("id_perfil_ics") String perfil, 
			@PathParam("id_canvas_ics") String canvas, 
			@PathParam("id_quadrante") String quadrante, 
			@PathParam("id_cor") String cor, 
			Postit p){
		
		try{
			Response resp = null;
			
			if(perfil != null && canvas != null && quadrante != null & cor != null && p != null){
				Perfil pe = perfilService.obterId(Long.valueOf(perfil));
				Canvas c = canvasService.obterPorId(Long.valueOf(canvas));
				
				if(verificaCamposObrigatorios(p) && pe != null && c != null){
					p.setDataUltimaNotificacao(new Date());
					p.setCanvas(c);
					p.setPerfil(pe);
					p = postitService.estruturaPostitQuadranteCor(quadrante, cor, p);
					p = postitService.salvarPostit(p);
					
					JSONObject json = new JSONObject();
					json.put("idPostitIcs", p.getId());
					json.put("dataUltimaNotificacao", p.getDataUltimaNotificacao().getTime());
					
					resp = Response.status(Status.OK).entity(json.toString()).build();
					
				} else
					resp = Response.status(Status.BAD_REQUEST).build();
				
			} else
				resp = Response.status(Status.BAD_REQUEST).build();
			
			return resp;
		}catch (Exception e){
			throw new WebApplicationException(Status.NOT_FOUND);
		}
	}
	
	private boolean verificaCamposObrigatorios(Postit p) {
		if(p.getDataCriacao() != null && p.getDataUltimaAtualizacao() != null){
			return true;
		} else {
			return false;
		}
	}
}
